<!-- 蓝色登录页面2 -->
<template>
  <view class="container bgc_force">
    <view class="header">
      <view class="title">
        您好，
        <br/>
        欢迎来到修改密码页
      </view>
    </view>
    <view class="login-view">
      <view class="t-login">
        <view class="form_class">
          <u--form :model="form" :rules="rules" ref="uForm">
            <view class="t-a">
              <text class="txt">账号</text>
              <u-form-item prop="username" borderBottom>
                <u--input v-model="form.username" placeholder="请输入您的账号"
                          clearable border="none"/>
              </u-form-item>
            </view>
            <view class="t-a">
              <text class="txt">原始密码</text>
              <u-form-item prop="oldPassword" borderBottom>
                <u--input type="password" v-model="form.oldPassword" placeholder="请输入您的原始密码"
                          clearable border="none"/>
              </u-form-item>
            </view>
            <view class="t-a">
              <text class="txt">新密码</text>
              <u-form-item prop="password" borderBottom>
                <u--input type="password" v-model="form.password" placeholder="请输入您的密码"
                          clearable border="none"/>
              </u-form-item>
            </view>
            <view class="t-a">
              <text class="txt">确认新密码</text>
              <u-form-item prop="password2" borderBottom>
                <u--input type="password" v-model="form.password2" placeholder="请输入您的密码"
                          clearable border="none"/>
              </u-form-item>
            </view>
          </u--form>
          <view class="mt-40">
            <view class="login_class" @tap="changePassword()">确定修改</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import {InfoUtils, UniRouter} from "@/util/uniUtils";
import RouterPath from "@/constant/RouterPath";

export default {
  name: 'login',
  data() {
    return {
      form: {
        username: 'clean1',
        password: '123456',
        oldPassword: '123456',//旧密码
        password2: '123456',//新密码确认
      },
      rules: {
        username: {
          // type: 'number',
          required: true,
          // regexp: !/^1[345789][0-9]{9}$/,
          // len: 11,
          message: '请填写账号',
          trigger: ['change', 'blur'],
        },
        oldPassword: {
          required: true,
          message: '请输入原始密码',
          trigger: ['change', 'blur'],
        },
        password: [
          {
            required: true,
            message: '请输入新密码',
            trigger: ['change', 'blur'],
          },
          {
            validator: (rule, value, callback) => {
              this.$refs.uForm.validateField('password2')
              return true
            },
            // message: '与下面的密码不一致',
            trigger: ['change', 'blur'],
          }
        ],
        password2: [
          {
            required: true,
            message: '请确认新密码',
            trigger: ['change', 'blur'],
          },
          {
            validator: (rule, value, callback) => {
              return value === this.form.password
            },
            message: '新密码两次输入不一致',
            trigger: ['change', 'blur'],
          }
        ]
      },
    };
  },
  onReady() {
    //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    this.$refs.uForm.setRules(this.rules)
  },
  onLoad() {
  },
  methods: {
    async changePassword() {
      try {
        //手动校验一下表单字段
        this.$refs.uForm.validateField('password')
        //整个表单校验
        await this.$refs.uForm.validate()
        InfoUtils.loading('修改中')
        let result = await this.$store.dispatch("ChangePassword",this.form)
        InfoUtils.showInfo(result)
        InfoUtils.hideLoading()
        //退出登录
        await this.$store.dispatch("Logout")
        UniRouter.redirectTo(RouterPath.login.login)
      } catch (e) {
        console.log(e)
        InfoUtils.showInfo(e)
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.header {
  width: 100%;
  height: 450rpx;
  background-image: linear-gradient(to right, $main-color, $main-color-2);

  .title {
    text-align: left;
    font-size: 42rpx;
    color: #ffffff;
    padding: 130rpx 0 0 70rpx;
    font-weight: bold;
    line-height: 70rpx;
  }
}


.login-view {
  width: 100%;
  position: relative;
  margin-top: -120rpx;
  background-color: $uni-bg-color;
  border-radius: 8% 8% 0 0;

  .t-login {
    width: 80%;
    //min-width: 600rpx;
    margin: 0 auto;
    font-size: 28rpx;
    padding-top: 80rpx;

    .form_class {
      zoom: 1;

      &:after {
        clear: both;
        display: block;
        visibility: hidden;
        height: 0;
        content: '\20';
      }

      .t-a {
        position: relative;
        margin-bottom: 20rpx;

        .txt {
          font-size: 32rpx;
          font-weight: bold;
          color: $uni-text-color;
        }

        input {
          height: 90rpx;
          line-height: 90rpx;
          margin-bottom: 50rpx;
          border-bottom: 1px solid #e9e9e9;
          font-size: 28rpx;
        }
      }
    }

    .login_class {
      font-size: 28rpx;
      background-image: linear-gradient(to left, $main-color, $main-color-2);
      color: #fff;
      height: 90rpx;
      line-height: 90rpx;
      border-radius: 50rpx;
      font-weight: bold;
      text-align: center;
    }

    .register_class {
      font-size: 28rpx;
      color: $uni-text-color;
      height: 90rpx;
      line-height: 90rpx;
      border-radius: 50rpx;
      font-weight: bold;
      background: $uni-bg-color-grey;
      text-align: center;
      margin-top: 30rpx;
    }
  }
}


.mt-40 {
  margin-top: 40rpx;
}


</style>
